package com.example.cloudalibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author chensixiang chensixiang1234@gmail.com
 */
@RestController
@Slf4j
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA() {
        // try {
        //     TimeUnit.MILLISECONDS.sleep(800);
        // } catch (InterruptedException e) {
        //     e.printStackTrace();
        // }
        return "-------------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "--------------testB";
    }

    @GetMapping("/testD")
    public String testD() {
        // 暂停几秒中线程
        // try {
        //     TimeUnit.SECONDS.sleep(2);
        // } catch (InterruptedException e) {
        //     e.printStackTrace();
        // }
        int i = 1 / 0;
        // log.info("testD 测试RT");
        log.info("testD 异常比例");
        return "--------------testD";
    }

    @GetMapping("testHotKey")
    @SentinelResource(value = "testHotKey", blockHandler = "del_testHotKey")
    // 处理Sentinel控制台配置的违规情况，不包括RuntimeException
    public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                             @RequestParam(value = "p2", required = false) String p2) {
        int i =1 / 0;
        return "testHotKey";
    }

    public String del_testHotKey(@RequestParam(value = "p1", required = false) String p1,
                                 @RequestParam(value = "p2", required = false) String p2,
                                 BlockException e) {
        return "del_testHotKey";
    }
}
